package io.renren.modules.es.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.ObjectUtil;
import io.renren.modules.combat.entity.TFightInformationEntity;
import io.renren.modules.combat.entity.TFightPeopleEntity;
import io.renren.modules.combat.service.TFightInformationService;
import io.renren.modules.combat.service.TFightPeopleService;
import io.renren.modules.es.vo.*;
import io.renren.modules.generator.entity.TTrainingEntity;
import io.renren.modules.generator.entity.TTrainingPeopleEntity;
import io.renren.modules.generator.service.TTrainingPeopleService;
import io.renren.modules.generator.service.TTrainingService;
import io.renren.modules.user.entity.*;
import io.renren.modules.user.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 初始化es job
 * @author captain
 * @date 2019-09-24
 */
@Slf4j
@EnableScheduling
@Component
public class InitESJob {

    @Autowired
    private ElasticsearchTemplate elsTemplate;

    @Autowired
    private TPeopleChangeService tPeopleChangeService;

    @Autowired
    private TPeopleService tPeopleService;

    @Autowired
    private TFightInformationService tFightInformationService;

    @Autowired
    private TFightPeopleService tFightPeopleService;

    @Autowired
    private TPeopleNoticeService tPeopleNoticeService;

    @Autowired
    private TPeopleOutService tPeopleOutService;

    @Autowired
    private TPeopleResumeService tPeopleResumeService;

    @Autowired
    private TTrainingPeopleService trainingPeopleService;

    @Autowired
    private TTrainingService trainingService;

    /**
     * 头衔
     */
    @Autowired
    private TPeopleDictionaryService tPeopleDictionaryService;

    /**
     * 单位
     */
    @Autowired
    private TPeopleFireDetachmentService tPeopleFireDetachmentService;

    /**
     * 初始化es
     * 每天跑一次
     */
    @Scheduled(cron = "0 0 0 * * ?")
//    @Scheduled(cron = "0 0/5 * * * ?")
    public void esInitJob() {
        log.info("esInitJob begin.....................................");
        elsTemplate.deleteIndex(FightInformationESVo.class);
        elsTemplate.deleteIndex(FightPeopleESVo.class);
        elsTemplate.deleteIndex(PeopleESVo.class);
        elsTemplate.deleteIndex(PeopleChangeESVo.class);
        elsTemplate.deleteIndex(PeopleNoticeESVo.class);
        elsTemplate.deleteIndex(PeopleOutESVo.class);
        elsTemplate.deleteIndex(PeopleResumeESVo.class);
        elsTemplate.deleteIndex(TrainingPeopleESVo.class);
        elsTemplate.deleteIndex(TrainingESVo.class);
        QueryWrapper<TPeopleDictionaryEntity> peopleDictionaryQueryWrapper = new QueryWrapper<>();
        peopleDictionaryQueryWrapper.select(TPeopleDictionaryEntity.class, info -> !info.getColumn().equals("order"));
        List<TPeopleDictionaryEntity> peopleDictionaryList = tPeopleDictionaryService.list(peopleDictionaryQueryWrapper);
        QueryWrapper<TPeopleFireDetachmentEntity> peopleFireDetachmentQueryWrapper = new QueryWrapper<>();
        peopleFireDetachmentQueryWrapper.select(TPeopleFireDetachmentEntity.class, info -> !info.getColumn().equals("explain"));
        List<TPeopleFireDetachmentEntity> detachmentList = tPeopleFireDetachmentService.list(peopleFireDetachmentQueryWrapper);
        List<TFightInformationEntity> fightInformationList = tFightInformationService.list();
        List<TTrainingEntity> trainingList = trainingService.list();
        Map<Integer, TPeopleDictionaryEntity> dictionaryMap = peopleDictionaryList.stream().collect(Collectors.toMap(v -> v.getId(), v -> v));
        Map<Integer, TPeopleFireDetachmentEntity> detachmentMap = detachmentList.stream().collect(Collectors.toMap(v -> v.getId(), v -> v));
        Map<Integer, TFightInformationEntity> informationMap = fightInformationList.stream().collect(Collectors.toMap(v -> v.getId(), v -> v));
        Map<Integer, TTrainingEntity> trainingMap = trainingList.stream().collect(Collectors.toMap(v -> v.getId(), v -> v));
        log.info("es init peopleChangeESVo begin.................");
        List<TPeopleChangeEntity> peopleChangeList = tPeopleChangeService.list();
        for(TPeopleChangeEntity tPeopleChangeEntity : peopleChangeList){
            PeopleChangeESVo peopleChangeESVo = ObjectUtil.source2Target(tPeopleChangeEntity, PeopleChangeESVo.class);
            if(tPeopleChangeEntity.getChangeTime() != null){
                peopleChangeESVo.setChangeTime(DateUtils.format(tPeopleChangeEntity.getChangeTime(),DateUtils.DATE_PATTERN));
            }
            IndexQuery indexQuery = new IndexQueryBuilder().withObject(peopleChangeESVo).build();
            elsTemplate.index(indexQuery);
        }
        log.info("es init peopleChangeESVo end.................");
        log.info("es init fightInformationESVo begin.................");
        for(TFightInformationEntity tFightInformationEntity : fightInformationList){
            FightInformationESVo fightInformationESVo = ObjectUtil.source2Target(tFightInformationEntity, FightInformationESVo.class);
            if(tFightInformationEntity.getTime() != null){
                fightInformationESVo.setTime(DateUtils.format(tFightInformationEntity.getTime(),DateUtils.DATE_PATTERN));
            }
            IndexQuery indexQuery = new IndexQueryBuilder().withObject(fightInformationESVo).build();
            elsTemplate.index(indexQuery);
        }
        log.info("es init fightInformationESVo end.................");
        log.info("es init fightPeopleESVo begin.................");
        List<TFightPeopleEntity> fightPeopleList = tFightPeopleService.list();
        for(TFightPeopleEntity tFightPeopleEntity : fightPeopleList){
            FightPeopleESVo fightPeopleESVo = ObjectUtil.source2Target(tFightPeopleEntity, FightPeopleESVo.class);
            if(tFightPeopleEntity.getFightId() != null){
                TFightInformationEntity fightInformationEntity = informationMap.get(tFightPeopleEntity.getFightId());
                if(fightInformationEntity != null){
                    fightPeopleESVo.setFightName(fightInformationEntity.getName());
                    if(fightInformationEntity.getTime() != null){
                        fightPeopleESVo.setFightBeginTime(DateUtils.format(fightInformationEntity.getTime()));
                    }
                    if(fightInformationEntity.getOffTime() != null){
                        fightPeopleESVo.setFightEndTime(DateUtils.format(fightInformationEntity.getOffTime()));
                    }
                }
            }
            IndexQuery indexQuery = new IndexQueryBuilder().withObject(fightPeopleESVo).build();
            elsTemplate.index(indexQuery);
        }
        log.info("es init fightPeopleESVo end.................");
        log.info("es init peopleESVo begin.................");
        List<TPeopleEntity> peopleList = tPeopleService.list();
        for(TPeopleEntity tPeopleEntity : peopleList){
            PeopleESVo peopleESVo = ObjectUtil.source2Target(tPeopleEntity, PeopleESVo.class);
            if(tPeopleEntity.getDept() != null){
                TPeopleFireDetachmentEntity detachmentEntity = detachmentMap.get(Integer.parseInt(tPeopleEntity.getDept()));
                if(detachmentEntity != null){
                    peopleESVo.setDeptName(detachmentEntity.getDetachment());
                }
            }
            if(tPeopleEntity.getClasses() != null){
                TPeopleDictionaryEntity dictionaryEntity = dictionaryMap.get(tPeopleEntity.getClasses());
                if(dictionaryEntity != null){
                    peopleESVo.setClassesName(dictionaryEntity.getName());
                }
            }
            IndexQuery indexQuery = new IndexQueryBuilder().withObject(peopleESVo).build();
            elsTemplate.index(indexQuery);
        }
        log.info("es init peopleESVo end.................");
        log.info("es init peopleNoticeESVo begin.................");
        List<TPeopleNoticeEntity> peopleNoticeList = tPeopleNoticeService.list();
        for(TPeopleNoticeEntity tPeopleNoticeEntity : peopleNoticeList){
            PeopleNoticeESVo peopleNoticeESVo = ObjectUtil.source2Target(tPeopleNoticeEntity, PeopleNoticeESVo.class);
            IndexQuery indexQuery = new IndexQueryBuilder().withObject(peopleNoticeESVo).build();
            if(tPeopleNoticeEntity.getChanageBefore() != null){
                TPeopleDictionaryEntity dictionaryEntity = dictionaryMap.get(tPeopleNoticeEntity.getChanageBefore());
                if(dictionaryEntity != null){
                    peopleNoticeESVo.setChanageBefore(dictionaryEntity.getName());
                }
            }
            if(tPeopleNoticeEntity.getChanageAfter() != null){
                TPeopleDictionaryEntity dictionaryEntity = dictionaryMap.get(tPeopleNoticeEntity.getChanageAfter());
                if(dictionaryEntity != null){
                    peopleNoticeESVo.setChanageAfter(dictionaryEntity.getName());
                }
            }
            elsTemplate.index(indexQuery);
        }
        log.info("es init peopleNoticeESVo end.................");
        log.info("es init peopleOutESVo begin.................");
        List<TPeopleOutEntity> peopleOutList = tPeopleOutService.list();
        for(TPeopleOutEntity tPeopleOutEntity : peopleOutList){
            PeopleOutESVo peopleOutESVo = ObjectUtil.source2Target(tPeopleOutEntity, PeopleOutESVo.class);
            if(tPeopleOutEntity.getOutTime() != null){
                peopleOutESVo.setOutTime(DateUtils.format(tPeopleOutEntity.getOutTime(),DateUtils.DATE_TIME_PATTERN));
            }
            IndexQuery indexQuery = new IndexQueryBuilder().withObject(peopleOutESVo).build();
            elsTemplate.index(indexQuery);
        }
        log.info("es init peopleOutESVo end.................");
        log.info("es init peopleResumeESVo begin.................");
        List<TPeopleResumeEntity> peopleResumeList = tPeopleResumeService.list();
        for(TPeopleResumeEntity tPeopleResumeEntity : peopleResumeList){
            PeopleResumeESVo peopleResumeESVo = ObjectUtil.source2Target(tPeopleResumeEntity, PeopleResumeESVo.class);
            if(tPeopleResumeEntity.getStartTime() != null){
                peopleResumeESVo.setStartTime(DateUtils.format(tPeopleResumeEntity.getStartTime()));
            }
            if(tPeopleResumeEntity.getEndTime() != null){
                peopleResumeESVo.setEndTime(DateUtils.format(tPeopleResumeEntity.getEndTime()));
            }
            IndexQuery indexQuery = new IndexQueryBuilder().withObject(peopleResumeESVo).build();
            elsTemplate.index(indexQuery);
        }
        log.info("es init peopleResumeESVo end.................");
        log.info("es init trainingESVo begin.................");
        for(TTrainingEntity tTrainingEntity : trainingList){
            TrainingESVo trainingESVo = ObjectUtil.source2Target(tTrainingEntity, TrainingESVo.class);
            if(tTrainingEntity.getStartTime() != null){
                trainingESVo.setStartTime(DateUtils.format(tTrainingEntity.getStartTime()));
            }
            if(tTrainingEntity.getEndTime() != null){
                trainingESVo.setEndTime(DateUtils.format(tTrainingEntity.getEndTime()));
            }
            IndexQuery indexQuery = new IndexQueryBuilder().withObject(trainingESVo).build();
            elsTemplate.index(indexQuery);
        }
        log.info("es init trainingESVo end.................");
        log.info("es init trainingPeopleESVo begin.................");
        List<TTrainingPeopleEntity> trainingPeopleList = trainingPeopleService.list();
        for(TTrainingPeopleEntity tTrainingPeopleEntity : trainingPeopleList){
            TrainingPeopleESVo trainingPeopleESVo = ObjectUtil.source2Target(tTrainingPeopleEntity, TrainingPeopleESVo.class);
            if(tTrainingPeopleEntity.getTrainingId() != null){
                TTrainingEntity trainingEntity = trainingMap.get(tTrainingPeopleEntity.getTrainingId());
                if(trainingEntity != null){
                    if(trainingEntity.getStartTime() != null){
                        trainingPeopleESVo.setTrainingBeginTime(DateUtils.format(trainingEntity.getStartTime()));
                    }
                    if(trainingEntity.getEndTime() != null){
                        trainingPeopleESVo.setTrainingEndTime(DateUtils.format(trainingEntity.getEndTime()));
                    }
                    trainingPeopleESVo.setTrainingName(trainingEntity.getName());
                }
            }
            IndexQuery indexQuery = new IndexQueryBuilder().withObject(trainingPeopleESVo).build();
            elsTemplate.index(indexQuery);
        }
        log.info("es init trainingPeopleESVo end.................");
        log.info("esInitJob end.....................................");
    }

}
